import React from 'react';
import {injectIntl} from '../intl';
import {Table} from 'antd';
import PropTypes from 'prop-types';
import routes from '../routes';
import {
    PAGE,
    TABLE,
    PAGINATION_OPTIONS,
    COMMON_TABLE_QUERY_FAIL_TEXT,
    COMMON_TABLE_EMPTY_TEXT,
    DATETIME_FORMAT,
} from '../../constants';
import {Link} from 'react-router-dom';
import {formatDateTime, conventSorter} from '../../utils';
import DropdownMenu from 'Shared/components/DropdownMenu';

export class AutoApproveHisTablePanel extends React.PureComponent {
    state = {
        loading: false
    };
    componentDidMount() {
        this.props.getInfo();
    }
    onChangeSort = (pagination, filters, sorter) => {
        if(pagination.current - 1 === this.props.pageIndex && pagination.pageSize === this.props.pageSize)
            this.props.onChangeSort({...conventSorter(sorter),
                pageIndex: PAGE.index});
    };
    
    render() {
        const columns = [
            {
                title: this.props.intl.formatMessage({
                    id: 'autoApproveHisTablePanel.column.approveTime',
                    defaultMessage: '审核时间'
                }),
                dataIndex: 'approveTime',
                render: text => formatDateTime(text, DATETIME_FORMAT)
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'autoApproveHisTablePanel.column.totalQTY',
                    defaultMessage: '总计数量'
                }),
                sorter: true,
                dataIndex: 'totalQTY',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'autoApproveHisTablePanel.column.passQTY',
                    defaultMessage: '通过数量'
                }),
                dataIndex: 'passQTY',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'autoApproveHisTablePanel.column.rejectedQTY',
                    defaultMessage: '拒赔数量'
                }),
                dataIndex: 'rejectedQTY'
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'autoApproveHisTablePanel.column.turnDownQTY',
                    defaultMessage: '驳回数量'
                }),
                dataIndex: 'turnDownQTY',
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'autoApproveHisTablePanel.column.notPassQTY',
                    defaultMessage: '需人工审核量'
                }),
                dataIndex: 'notPassQTY'
            },
            {
                title: this.props.intl.formatMessage({
                    id: 'autoApproveHisTablePanel.column.operation',
                    defaultMessage: '操作'
                }),
                dataIndex: 'id',
                key: 'id',
                width: 80,
                fixed: 'right',
                render: (text, r) => {
                    const menus = [{
                        id: 'autoDetail',
                        children: <Link key="autoDetail" to={routes.autoDetail.format(r.id)}>{
                            this.props.intl.formatMessage({
                                id: 'autoApproveHisTablePanel.autoDetail',
                                defaultMessage: '详情'
                            })
                        }</Link>,
                        hidden: false,
                    }];
                    return <DropdownMenu key={r.id} menus={menus} primaryLength={1} id={r.id} />;
                }
            }
        ];
        const pagination = {
            total: this.props.total,
            pageSize: this.props.pageSize,
            current: this.props.pageIndex + 1,
            onShowSizeChange: this.props.onPageSizeChange,
            onChange: this.props.onPageIndexChange,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <div>
                <Table
                    className="white-space-nowrap"
                    columns={columns}
                    dataSource={this.props.data}
                    rowKey="id"
                    pagination={pagination}
                    loading={this.props.loading || this.state.loading}
                    onChange={this.onChangeSort}
                    locale={locale}
                    {...TABLE}/>
            </div>
        );
    }
}
AutoApproveHisTablePanel.propTypes = {
    data: PropTypes.array.isRequired,
    errorMessage: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    onPageIndexChange: PropTypes.func.isRequired,
    onPageSizeChange: PropTypes.func.isRequired,
    intl: PropTypes.object,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number
};
import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {
    getAutoList,
    tableAutoSearch
} from '../actions';
const getData = selectorFactory(['page', 'domainData', 'autoList', 'data']);
const mapStateToProps = state => ({
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'autoList', 'isFetching']),
    errorMessage: state.getIn(['page', 'domainData', 'autoList', 'message']),
    total: state.getIn(['page', 'domainData', 'autoList', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'autoQueryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'autoQueryBySort', 'pageSize']),
});

const mapDispatchToProps = dispatch => ({
    getInfo: () => dispatch(getAutoList()),
    onPageSizeChange: (pageIndex, pageSize) =>
        dispatch(
            tableAutoSearch({
                pageIndex: PAGE.index,
                pageSize
            })
        ),
    onPageIndexChange: pageIndex =>
        dispatch(
            tableAutoSearch({
                pageIndex: pageIndex - 1
            })
        ),
    onChangeSort: option => dispatch(tableAutoSearch(option)),
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(injectIntl(AutoApproveHisTablePanel));
